#include "login.h"
#include "ui_login.h"
#include "log/log_init.h"
#include "dbio/db_manager.h"
#include <QCryptographicHash>
Login::Login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);

    this->setWindowFlags(Qt::FramelessWindowHint);
    //设置默认的焦点位置
    ui->user_edit->setFocus();
    init();
}

//实现窗口移动
void Login::mouseMoveEvent(QMouseEvent *event)
{
    if(mouse_press)
    {
        QPoint move_pos=event->globalPos();
        this->move(move_pos-move_point);
    }
}
void Login::mousePressEvent(QMouseEvent *event)
{
    //this->setCursor(Qt::ClosedHandCursor);//改变鼠标样式
    if(event->button()==Qt::LeftButton)
    {
        mouse_press=true;
    }
    if(event->pos().y()<=50 )
        move_point=event->globalPos()-this->pos();
    else
        mouse_press=false;
}

Login::~Login()
{
    delete ui;
}

void Login::on_min_btn_clicked()
{
    setWindowState(Qt::WindowMinimized);
}

void Login::on_close_btn_clicked()
{
    QApplication::quit();
}

void Login::on_login_btn_clicked()
{
    auto user = ui->user_edit->text();
    auto pwd = ui->pwd_edit->text();
    checkPassword(user,pwd);
}

void Login::init()
{
    QList<QList<QString>> userList = ContrlLogic::getInstance()->selectAllUserInfo();
    for(auto it :userList){
        UserInfo info;
        info.id                    =    it.at(CMF_USER_ID).toInt();
        info.user_type             =    it.at(CMF_USER_USER_TYYE).toInt();
        info.sex                   =    it.at(CMF_USER_SEX).toInt();
        info.birthday              =    it.at(CMF_USER_BIRTHDAY);
        info.last_login_time       =    it.at(CMF_USER_LAST_LOGIN_TIME);
        info.score                 =    it.at(CMF_USER_SCORE);
        info.coin                  =    it.at(CMF_USER_COIN);
        info.balance               =    it.at(CMF_USER_BALANCE);
        info.create_time           =    it.at(CMF_USER_CREATE_TIME);
        info.user_status           =    it.at(CMF_USER_USER_STATUS);
        info.user_login            =    it.at(CMF_USER_USER_LOGIN);
        info.user_pass             =    it.at(CMF_USER_USER_PASS);
        info.user_nickname         =    it.at(CMF_USER_USER_NICKNAME);
        info.user_email            =    it.at(CMF_USER_USER_EMAIL);
        info.user_url              =    it.at(CMF_USER_USER_URL);
        info.user_job             =    it.at(CMF_USER_USER_JOB);
        info.avatar                =    it.at(CMF_USER_AVATAR);
        info.signature             =    it.at(CMF_USER_SIGNATURE);
        info.last_login_ip         =    it.at(CMF_USER_LAST_LOGIN_IP);
        info.user_activation_key   =    it.at(CMF_USER_USER_ACTIVATION_KEY);
        info.mobile                =    it.at(CMF_USER_MOBILE);
        info.more                  =    it.at(CMF_USER_MORE);
        info.dept_id               =    it.at(CMF_USER_DEPT_ID).toInt();
        info.dept_path             =    it.at(CMF_USER_DEPT_PATH);

        userInfo_[it.at(CMF_USER_ID)]= info;
    }
    QLOG_INFO()<<"init users total = "<<userList.size();
}
//QString md5Str = QCryptographicHash::hash(str.toLatin1(),QCryptographicHash::Md5).toHex();
void Login::checkPassword(const QString &name, const QString &pwd)
{
    QString p1 = QCryptographicHash::hash(QString("fIn2KJSlVFfrtNQQbP"+pwd).toLatin1(),QCryptographicHash::Md5).toHex();
    QString p2 = QCryptographicHash::hash(p1.toLatin1(),QCryptographicHash::Md5).toHex();
    QLOG_INFO()<<"pwd = "<<"###"<<p2;
    for(auto it : userInfo_){
        if(it.user_login == name && it.user_pass == ("###"+p2)){
            loginUserInfo_ = it;
            QLOG_INFO()<<"login sucess id = "<<loginUserInfo_.id<<"name = "<<loginUserInfo_.user_login;
            if(mainWindow_ == nullptr){
                mainWindow_ = new MainWindow;
            }
            mainWindow_->show();
            this->hide();
        }else{
            ui->pwd_edit->clear();
            ui->login_status->setText("账号或密码错误!");
        }
    }
}

void Login::on_pwd_edit_returnPressed()
{
    on_login_btn_clicked();
}
